package com.zzy.tliaswebmanagement.controller;


import com.zzy.tliaswebmanagement.pojo.Dept;
import com.zzy.tliaswebmanagement.pojo.Result;
import com.zzy.tliaswebmanagement.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {


    //private static Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result list() {

        log.info("查询全部部门数据");

        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }


//删除部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){ //获取路径中的id用@PathVairable
        log.info("根据ID删除部门：{}",id);
        deptService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Dept dept){  //json格式的数据要想封装到这个对象当中，需要用@RequestBody来转换
        log.info("新增数部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result find(@PathVariable Integer id){
        log.info("根据ID查询部门{}",id);
        Dept dept = deptService.find(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
